#include "settingPage.h"

static lv_style_t style_box;

extern lv_group_t *key_group; //键盘组
extern TaskMgr task_mgr;
A_I_B infoBox;
lv_obj_t* cont[4];
lv_obj_t* chooseCont;
lv_obj_t* profileCircle;
lv_obj_t* img[10];
lv_obj_t* slider;
lv_obj_t* LED;

LV_IMG_DECLARE(ye);
LV_IMG_DECLARE(ganyu);
LV_IMG_DECLARE(babala);
LV_IMG_DECLARE(nuoaier);
LV_IMG_DECLARE(paimeng);
LV_IMG_DECLARE(wanye);
LV_IMG_DECLARE(wendi);
LV_IMG_DECLARE(yanfei);
LV_IMG_DECLARE(leishen);
LV_IMG_DECLARE(feixieer);
LV_IMG_DECLARE(pmaru);

static void slider_event_cb(lv_obj_t* slider, lv_event_t e)
{
    // if (e == LV_EVENT_VALUE_CHANGED) {
    //     int cur = lv_slider_get_value(slider);
    //     cur = (int)(cur*0.3);
    //     changeVol(cur);
    // }

}
static void setPage_event_callback(struct _lv_obj_t *obj, lv_event_t event)
{
    
    if (event == LV_EVENT_KEY) {
        const uint32_t *key = lv_event_get_data();
        if (*key == LV_KEY_UP) {
            if(chooseCont==cont[1]){//当前处于头像框
                if(lv_obj_get_y(profileCircle)==247){//当前处于头像框第一行，此时要切换框
                    lv_obj_set_style_local_border_width(cont[1],LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,0);
                    lv_obj_set_style_local_border_width(cont[0],LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,5);
                    chooseCont = cont[0];
                }else{//头像框往走上一行
                    lv_obj_set_pos(profileCircle,lv_obj_get_x(profileCircle),247);
                    infoBox.chooseProfile-=5;
                }
                
            }
        } else if (*key == LV_KEY_DOWN) {
            if(chooseCont==cont[0]){//当前处于音量框，要切换到头像框
                lv_obj_set_style_local_border_width(cont[0],LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,0);
                lv_obj_set_style_local_border_width(cont[1],LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,5);
                chooseCont = cont[1];
            }else{
                if(lv_obj_get_y(profileCircle)==247){//当前处于头像框第一行，要向下走一行
                    lv_obj_set_pos(profileCircle,lv_obj_get_x(profileCircle),332);
                    infoBox.chooseProfile+=5;
                }
            }
        } else if (*key == LV_KEY_LEFT) {
            if(chooseCont==cont[0]){//当前处于音量框
                if(lv_slider_get_value(slider)!=0){
                    infoBox.volumn-=10;
                    lv_slider_set_value(slider,lv_slider_get_value(slider)-10,LV_ANIM_OFF);
                    int cur = infoBox.volumn;
                    cur = (int)(cur*0.3);
                    changeVol(cur);
                }
            }
            if(chooseCont==cont[1]){//当前处于头像框
                if(lv_obj_get_x(profileCircle)!=45){//不是最左边
                    lv_obj_set_pos(profileCircle,lv_obj_get_x(profileCircle)-97,lv_obj_get_y(profileCircle));
                    infoBox.chooseProfile-=1;
                }
            }
        } else if (*key == LV_KEY_RIGHT) {
            if(chooseCont==cont[0]){//当前处于音量框
                if(lv_slider_get_value(slider)!=100){
                    lv_slider_set_value(slider,lv_slider_get_value(slider)+10,LV_ANIM_OFF);
                    infoBox.volumn+=10;
                    int cur = infoBox.volumn;
                    cur = (int)(cur*0.3);
                    changeVol(cur);
                }
                    
            }
            if(chooseCont==cont[1]){//当前处于头像框
                if(lv_obj_get_x(profileCircle)!=433){//不是最右边
                    lv_obj_set_pos(profileCircle,lv_obj_get_x(profileCircle)+97,lv_obj_get_y(profileCircle));
                    infoBox.chooseProfile+=1;
                }
            }
        } else if (*key == LV_KEY_ENTER) {
            if(lv_led_get_bright(LED)>160){
                lv_led_off(LED);
                stopSong();
                infoBox.enableBGM=0;
            }else{
                lv_led_on(LED);
                infoBox.enableBGM=1;
                playSong();
            } 
        } else if (*key == LV_KEY_HOME) {
            task_mgr.BackTo_CG_request = 1;
        }
    }
}

lv_obj_t* createSettingPage() {
    //样式
    lv_style_reset(&style_box);
    lv_style_set_value_align(&style_box, LV_STATE_DEFAULT, LV_ALIGN_OUT_TOP_LEFT);
    lv_style_set_value_ofs_y(&style_box, LV_STATE_DEFAULT, -LV_DPX(10));
    lv_style_set_margin_top(&style_box, LV_STATE_DEFAULT, LV_DPX(20));
    lv_style_set_bg_opa(&style_box, LV_STATE_DEFAULT, LV_OPA_50);

    //用于装载的对象
    lv_obj_t* mainPage = lv_obj_create(lv_scr_act(), NULL);
    lv_obj_set_style_local_radius(mainPage, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0);
    //lv_obj_set_style_local_bg_color(mainPage, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLUE);
    lv_obj_set_pos(mainPage, 0, 0);
    lv_obj_set_width(mainPage, 800);
    lv_obj_set_height(mainPage, 480);
    //底图
    lv_obj_t* imgGirl = lv_img_create(mainPage, NULL);
    lv_img_set_src(imgGirl, "F:/anime.bin");

    //底图上面创造容器
    cont[0] = lv_cont_create(mainPage, NULL);
    lv_cont_set_layout(cont[0], LV_LAYOUT_PRETTY_MID);
    lv_obj_add_style(cont[0], LV_CONT_PART_MAIN, &style_box);
    lv_cont_set_fit2(cont[0], LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_style_local_value_str(cont[0], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, "Basics Setting");
    lv_obj_set_width(cont[0], 400);
    //lv_obj_set_height(h, 100);
    lv_obj_set_pos(cont[0], 80, 30);
    lv_obj_set_style_local_border_color(cont[0],LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_GREEN);
    lv_obj_set_style_local_border_width(cont[0],LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,0);
    //滑动条
    slider = lv_slider_create(cont[0], NULL);
    lv_slider_set_value(slider, infoBox.volumn, LV_ANIM_OFF);
    lv_obj_set_event_cb(slider, slider_event_cb);
    lv_obj_set_width_margin(slider, 200);
    lv_obj_set_style_local_value_str(slider, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, "Volumn");
    //按钮
    lv_obj_t* btn = lv_btn_create(cont[0], NULL);
    lv_btn_set_fit2(btn, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_width(btn, 200);
    lv_obj_t* label = lv_label_create(btn, NULL);
    lv_btn_toggle(btn);
    lv_label_set_text(label, "OK nya!");

    //第二个容器
    cont[1] = lv_cont_create(mainPage, NULL);
    chooseCont = cont[1];//默认选中第二个框
    lv_cont_set_layout(cont[1], LV_LAYOUT_PRETTY_MID);
    lv_obj_add_style(cont[1], LV_CONT_PART_MAIN, &style_box);
    lv_cont_set_fit2(cont[1], LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_style_local_value_str(cont[1], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, "Profile change");
    lv_obj_set_width(cont[1], 500);
    //lv_obj_set_height(h, 150);
    lv_obj_set_pos(cont[1], 30, 230);
    lv_obj_set_style_local_border_color(cont[1],LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_GREEN);
    lv_obj_set_style_local_border_width(cont[1],LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,5);
    //头像 
    img[0] = lv_img_create(cont[1], NULL);
    lv_img_set_src(img[0],&ye);
    img[1] = lv_img_create(cont[1], NULL);
    lv_img_set_src(img[1], &paimeng);
    img[2] = lv_img_create(cont[1], NULL);
    lv_img_set_src(img[2], &nuoaier);
    img[3] = lv_img_create(cont[1], NULL);
    lv_img_set_src(img[3], &babala);
    img[4] = lv_img_create(cont[1], NULL);
    lv_img_set_src(img[4], &wendi);
    img[5] = lv_img_create(cont[1], NULL);
    lv_img_set_src(img[5], &ganyu);
    img[6] = lv_img_create(cont[1], NULL);
    lv_img_set_src(img[6], &wanye);
    img[7] = lv_img_create(cont[1], NULL);
    lv_img_set_src(img[7], &yanfei);
    img[8] = lv_img_create(cont[1], NULL);
    lv_img_set_src(img[8], &leishen);
    img[9] = lv_img_create(cont[1], NULL);
    lv_img_set_src(img[9], &feixieer);
    //选择关卡的框框
    profileCircle = lv_btn_create(mainPage, NULL);
    lv_obj_align(profileCircle, NULL, LV_ALIGN_IN_RIGHT_MID, -20, 0);
    lv_obj_set_style_local_bg_opa(profileCircle, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_10); // 设置不透明度
    lv_obj_set_size(profileCircle, 80, 80);
    //lv_obj_set_pos(profileCircle,45,247);
    //根据现有的选择设置框框
    if (infoBox.chooseProfile<=5)
    {
        lv_obj_set_pos(profileCircle,45+(infoBox.chooseProfile-1)*97,247);
    }else{
        lv_obj_set_pos(profileCircle,45+(infoBox.chooseProfile-6)*97,332);
    }
    
    //第三个容器
    cont[2] = lv_cont_create(mainPage, NULL);
    lv_cont_set_layout(cont[2], LV_LAYOUT_PRETTY_MID);
    lv_obj_add_style(cont[2], LV_CONT_PART_MAIN, &style_box);
    lv_cont_set_fit2(cont[2], LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_style_local_value_str(cont[2], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, "Other");
    lv_obj_set_width(cont[2], 200);
    //lv_obj_set_height(h, 150);
    lv_obj_set_pos(cont[2], 560, 30);
    //是否开启BGM的label
    lv_obj_t* SoundLabel = lv_label_create(cont[2],NULL);
    lv_label_set_text(SoundLabel, "The BGM is :");
    LED =lv_led_create(cont[2],NULL);
    if(infoBox.enableBGM)
        lv_led_on(LED);
    else
        lv_led_off(LED);
    //按钮绑定组
    lv_group_add_obj(key_group, btn);
    lv_obj_set_event_cb(btn, setPage_event_callback);//按钮的事件
    //第四个容器
    cont[3] = lv_cont_create(mainPage, NULL);
    lv_cont_set_layout(cont[3], LV_LAYOUT_PRETTY_MID);
    lv_obj_add_style(cont[3], LV_CONT_PART_MAIN, &style_box);
    lv_cont_set_fit2(cont[3], LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_style_local_value_str(cont[3], LV_CONT_PART_MAIN, LV_STATE_DEFAULT, "User Info");
    lv_obj_set_width(cont[3], 200);
    //lv_obj_set_height(h, 150);
    lv_obj_set_pos(cont[3], 560, 190);
    lv_obj_t *PMARU = lv_img_create(cont[3], NULL);
    lv_img_set_src(PMARU,&pmaru);
    lv_obj_t* UserName = lv_label_create(cont[3],NULL);
    lv_label_set_text(UserName, "User: Pmaru");

    return mainPage;
}

void settingInfoInit(){
    infoBox.chooseProfile=1;
    infoBox.volumn=80;
    infoBox.enableBGM=0;
    //load_settingData_toSD(&infoBox);
    read_settingData_fromSD(&infoBox);
}
void load_settingData_toSD(A_I_B *sd){
    
    FRESULT frl;
    FIL settingData;

    frl = f_open(&settingData, "0:settingData.bat", FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
    if (frl == FR_OK)
        UART_printf(&huart1, "'settingData.bat' create OK!");
    else
        UART_printf(&huart1, "file open error = %d\r\n", frl);
    
    frl = f_write(&settingData, sd, sizeof(A_I_B), NULL);
    if (frl == FR_OK)
        UART_printf(&huart1, "%d\n",sizeof(A_I_B));
    else
        UART_printf(&huart1, "file write error = %d\r\n", frl);
    
    f_close(&settingData);

    if (frl == FR_OK) {
        UART_printf(&huart1, "Doc Close OK!\r\n");
    } else
        UART_printf(&huart1, "Doc Close error = %d\r\n", frl);
}
void read_settingData_fromSD(A_I_B *sd){

    FRESULT frr;
    FIL settingData;

    frr = f_open(&settingData, "0:settingData.bat", FA_READ);
    if (frr == FR_OK)
        UART_printf(&huart1, "'settingData.bat' create OK!");
    else
        UART_printf(&huart1, "file open error = %d\r\n", frr);
    
    frr = f_read(&settingData, sd, sizeof(A_I_B), NULL);
    if (frr == FR_OK)
        UART_printf(&huart1, "%d\n",sizeof(A_I_B));
    else
        UART_printf(&huart1, "file write error = %d\r\n", frr);
    
    f_close(&settingData);

    if (frr == FR_OK) {
        UART_printf(&huart1, "Doc Close OK!\r\n");
    } else
        UART_printf(&huart1, "Doc Close error = %d\r\n", frr);
}
lv_img_dsc_t * choosedProfile(int num){
    switch (num)
    {
    case 1:
        return &ye;
        break;
    case 2:
        return &paimeng;
        break;
    case 3:
        return &nuoaier;
        break;
    case 4:
        return &babala;
        break;
    case 5:
        return &wendi;
        break;
    case 6:
        return &ganyu;
        break;
    case 7:
        return &wanye;
        break;
    case 8:
        return &yanfei;
        break;
    case 9:
        return &leishen;
        break;
    case 10:
        return &feixieer;
        break;
    default:
        break;
    }
}
